package com.isoftstone.disk.login.controller;

import com.isoftstone.disk.login.entity.MenuLevelEnetity;
import com.isoftstone.disk.login.service.LoginService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MainController {
    private Logger logger = LogManager.getLogger(this.getClass().getName());
    @Resource
    private LoginService loginService;
    @RequestMapping("/")
    public String main(HttpServletRequest request){

        HttpSession session = request.getSession();
        List<MenuLevelEnetity> list = loginService.queryMenuLevelByLangId();
        session.setAttribute("menu",list);
        return "login/main";
    }
    @RequestMapping("/getServiceMenu")
    @ResponseBody
    public List<MenuLevelEnetity> getServiceMenu(HttpServletRequest request){
        String menuId = request.getParameter("menuId");
        logger.info("menuId:" + menuId);
        HttpSession session = request.getSession();
        List<MenuLevelEnetity> list = (List<MenuLevelEnetity>) session.getAttribute("menu");
        for(MenuLevelEnetity menuLevelEnetity:list){
            if(menuId.equals(menuLevelEnetity.getMenuId())){
                return menuLevelEnetity.getChildList();
            }
        }
        return  null;
    }

    @RequestMapping("/welcome")
    public String welcome(){
        return "login/welcome";
    }
}
